<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Pay;
use app\common\model\Banner;
use app\common\model\Feedback;
use app\common\model\Goods as ModelGoods;
use app\common\model\goods\Goods as GoodsGoods;
use app\common\model\Hcode;
use app\common\model\Message;
use app\common\model\MessageRead;
use app\common\model\MoneyLog;
use app\common\model\order\Goods;
use app\common\model\order\goods\Log;
use app\common\model\order\Order;
use app\common\model\User;
use app\common\model\user\Group;
use app\common\model\user\Info;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index', 'scan_verify', 'company', 'cs'];
    protected $noNeedRight = ['*'];

    public function cs()
    {
        $order_ids = Order::where(['order_type' => '2', 'order_status' => ['>', 0]])->column('id');
        $Lists = Goods::where('order_id', 'in', $order_ids)->select();
        foreach ($Lists as $k => $v) {
            ModelGoods::where('id', $v['goods_id'])->setInc('sales', $v['goods_num']);
        }

        // Log::add_performance_detail($order,time());
        //    MoneyLog::commission($order);
    }
    /**
     * @Author LK
     * @date: 2023-11-10 
     * @ApiTitle (公司介绍)
     * @ApiMethod (GET)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="type", type="string", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function company()
    {
        $name = config('site.name');
        $logo = cdnurl(config('site.logo'), true);
        $start_page_image = cdnurl(config('site.start_page_image'), true);
        $company_introduction = config('site.company_introduction');
        $this->success('', compact('name', 'logo', 'start_page_image', 'company_introduction'));
    }
    /**
     * 首页
     *
     */
    public function index()
    {

        $banner = Banner::all();
        foreach ($banner as $key => $value) {
            $value->hidden(['type']);
        }
        //首页推荐
        $goods = GoodsGoods::where(['hot' => 1, 'type' => 1, 'status' => 'normal'])->order('weigh desc')->select();
        $this->success('请求成功', compact('banner', 'goods'));
    }
    /**
     * @Author LK
     * @date: 2023-10-20 
     * @ApiTitle (意见反馈)
     * @ApiMethod (POST)
     * @ApiParams (name="content", type="string", required=true, sample="", description="反馈")
     * @ApiParams (name="images", type="string", required=true, sample="", description="图片")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function submit_feedback()
    {
        $param = request()->param();
        extract($param);
        $param['user_id'] = $this->auth->id;
        $res = Feedback::create($param);
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
    /**
     * @Author LK
     * @date: 2023-10-23 
     * @ApiTitle (系统消息)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function message()
    {
        $lists = Message::paginate();
        $uid = $this->auth->id;
        foreach ($lists as $k => $v) {
            $v->is_read = MessageRead::where(['user_id' => $uid, 'm_id' => $v->id])->count() ? true : false;
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2023-10-23 
     * @ApiTitle (系统消息详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function message_info()
    {
        $id = request()->param('id');
        $lists = Message::get($id);
        $uid = $this->auth->id;
        $num = MessageRead::where(['user_id' => $uid, 'm_id' => $id])->count();
        if (!$num) {
            MessageRead::create([
                'user_id' => $uid,
                'm_id' => $id,
            ]);
        }
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @date: 2023-11-07 
     * @ApiTitle (扫码验证)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function scan_verify()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $data = Hcode::get(['id' => $id, 'status' => 'normal']);
        if ($data) {
            $data->status = 'hidden';
            $data->save();
            $data['h5_banner'] = cdnurl(Config('site.h5_banner'), true);
            $data['h5_text'] = Config('site.h5_text');
        }
        $this->success('', $data);
    }
}
